
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "../../support.h"
#include "gui_al.h"
#include "gui_io.h"
#include "undo.h"
#include "../../cryptography.h"
#include "../../gui_io.h"
#include "../../gui_al.h"
#include "../../error.h"
#include "../../structure.h"
#include "../../journal.h"
#include "../../settings.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)

void
on_ok_btn_activate                     (GtkButton       *button,
                                        gpointer         user_data);

void
on_cancel_btn_activate                 (GtkButton       *button,
                                        gpointer         user_data);

void
on_expand_local_activate               (GtkExpander     *expander,
                                        gpointer         user_data);

void
on_expand_file_activate                (GtkExpander     *expander,
                                        gpointer         user_data);

void
on_web_expand_activate                 (GtkExpander     *expander,
                                        gpointer         user_data);

GtkWidget *create_link_window(int *locs)
{

	GtkWidget *link_window;
	GtkWidget *vbox1;
	GtkWidget *main_lbl;
	GtkWidget *expand_local;
	GtkWidget *scrolledwindow1;
	GtkWidget *tree_root;
	GtkWidget *lbl_expand1;
	GtkWidget *expand_file;
	GtkWidget *hbox1;
	GtkWidget *localFIle_entry;
	GtkWidget *lbl_expand2;
	GtkWidget *web_expand;
	GtkWidget *vbox2;
	GtkWidget *hbox4;
	//GtkWidget *name_lbl;
	//GtkWidget *name_entry;
	GtkWidget *hbox3;
	GtkWidget *hyperlink_lbl;
	GtkWidget *link_entry;
	GtkWidget *lbl_expand3;
	GtkWidget *hbox2;
	GtkWidget *ok_btn;
	GtkWidget *cancel_btn;

	link_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(link_window), _("New Link"));
	gtk_window_set_default_size(GTK_WINDOW(link_window), 300, -1);

	vbox1 = gtk_vbox_new(FALSE, 0);
	gtk_widget_show(vbox1);
	gtk_container_add(GTK_CONTAINER(link_window), vbox1);

	main_lbl =
	    gtk_label_new(_("New Link Entry: Select a type and hit ok:"));
	gtk_widget_show(main_lbl);
	gtk_box_pack_start(GTK_BOX(vbox1), main_lbl, FALSE, FALSE, 0);

	expand_local = gtk_expander_new(NULL);
	gtk_widget_show(expand_local);
	gtk_box_pack_start(GTK_BOX(vbox1), expand_local, TRUE, TRUE, 0);

	scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_show(scrolledwindow1);
	gtk_container_add(GTK_CONTAINER(expand_local), scrolledwindow1);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW
					    (scrolledwindow1),
					    GTK_SHADOW_IN);

	tree_root = gtk_tree_view_new();
	gtk_widget_show(tree_root);
	gtk_container_add(GTK_CONTAINER(scrolledwindow1), tree_root);

	lbl_expand1 = gtk_label_new(_("Other Journal Entry"));
	gtk_widget_show(lbl_expand1);
	gtk_expander_set_label_widget(GTK_EXPANDER(expand_local),
				      lbl_expand1);

	expand_file = gtk_expander_new(NULL);
	gtk_widget_show(expand_file);
	gtk_box_pack_start(GTK_BOX(vbox1), expand_file, FALSE, FALSE, 0);

	hbox1 = gtk_hbox_new(FALSE, 0);
	gtk_widget_show(hbox1);
	gtk_container_add(GTK_CONTAINER(expand_file), hbox1);

	localFIle_entry = gtk_entry_new();
	gtk_widget_show(localFIle_entry);
	gtk_box_pack_start(GTK_BOX(hbox1), localFIle_entry, TRUE, TRUE, 0);


	lbl_expand2 = gtk_label_new(_("Link To a Search:"));
	gtk_widget_show(lbl_expand2);
	gtk_expander_set_label_widget(GTK_EXPANDER(expand_file),
				      lbl_expand2);

	web_expand = gtk_expander_new(NULL);
	gtk_widget_show(web_expand);
	gtk_box_pack_start(GTK_BOX(vbox1), web_expand, FALSE, FALSE, 0);

	vbox2 = gtk_vbox_new(FALSE, 0);
	gtk_widget_show(vbox2);
	gtk_container_add(GTK_CONTAINER(web_expand), vbox2);

	hbox4 = gtk_hbox_new(FALSE, 0);
	gtk_widget_show(hbox4);
	gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);

	/*name_lbl = gtk_label_new (_("Title:"));
	   gtk_widget_show (name_lbl);
	   gtk_box_pack_start (GTK_BOX (hbox4), name_lbl, FALSE, FALSE, 0);

	   name_entry = gtk_entry_new ();
	   gtk_widget_show (name_entry);
	   gtk_box_pack_start (GTK_BOX (hbox4), name_entry, TRUE, TRUE, 0); */

	hbox3 = gtk_hbox_new(FALSE, 0);
	gtk_widget_show(hbox3);
	gtk_box_pack_start(GTK_BOX(vbox2), hbox3, FALSE, FALSE, 0);

	hyperlink_lbl = gtk_label_new(_("Hyperlink:"));
	gtk_widget_show(hyperlink_lbl);
	gtk_box_pack_start(GTK_BOX(hbox3), hyperlink_lbl, FALSE, FALSE, 0);

	link_entry = gtk_entry_new();
	gtk_widget_show(link_entry);
	gtk_box_pack_start(GTK_BOX(hbox3), link_entry, TRUE, TRUE, 0);
	gtk_entry_set_text(GTK_ENTRY(link_entry), _("http://"));

	lbl_expand3 = gtk_label_new(_("Web Address / Website"));
	gtk_widget_show(lbl_expand3);
	gtk_expander_set_label_widget(GTK_EXPANDER(web_expand),
				      lbl_expand3);

	hbox2 = gtk_hbox_new(FALSE, 14);
	gtk_widget_show(hbox2);
	gtk_box_pack_start(GTK_BOX(vbox1), hbox2, FALSE, FALSE, 1);
	gtk_container_set_border_width(GTK_CONTAINER(hbox2), 3);

	ok_btn = gtk_button_new_from_stock("gtk-ok");
	gtk_widget_show(ok_btn);


	cancel_btn = gtk_button_new_from_stock("gtk-cancel");
	gtk_widget_show(cancel_btn);
	gtk_box_pack_start(GTK_BOX(hbox2), cancel_btn, FALSE, FALSE, 0);

	gtk_box_pack_start(GTK_BOX(hbox2), ok_btn, FALSE, FALSE, 0);

	g_signal_connect((gpointer) expand_local, "activate",
			 G_CALLBACK(on_expand_local_activate), locs);
	g_signal_connect((gpointer) expand_file, "activate",
			 G_CALLBACK(on_expand_file_activate), locs);
	g_signal_connect((gpointer) web_expand, "activate",
			 G_CALLBACK(on_web_expand_activate), locs);
	g_signal_connect((gpointer) ok_btn, "released",
			 G_CALLBACK(on_ok_btn_activate), locs);
	g_signal_connect((gpointer) cancel_btn, "released",
			 G_CALLBACK(on_cancel_btn_activate), locs);
	g_signal_connect((gpointer) ok_btn, "activate",
			 G_CALLBACK(on_ok_btn_activate), locs);
	g_signal_connect((gpointer) cancel_btn, "activate",
			 G_CALLBACK(on_cancel_btn_activate), locs);



	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF(link_window, link_window,
				   "link_window");
	GLADE_HOOKUP_OBJECT(link_window, vbox1, "vbox1");
	GLADE_HOOKUP_OBJECT(link_window, main_lbl, "main_lbl");
	GLADE_HOOKUP_OBJECT(link_window, expand_local, "expand_local");
	GLADE_HOOKUP_OBJECT(link_window, scrolledwindow1,
			    "scrolledwindow1");
	GLADE_HOOKUP_OBJECT(link_window, tree_root, "tree_root");
	GLADE_HOOKUP_OBJECT(link_window, lbl_expand1, "lbl_expand1");
	GLADE_HOOKUP_OBJECT(link_window, expand_file, "expand_file");
	GLADE_HOOKUP_OBJECT(link_window, hbox1, "hbox1");
	GLADE_HOOKUP_OBJECT(link_window, localFIle_entry,
			    "localFIle_entry");

	GLADE_HOOKUP_OBJECT(link_window, lbl_expand2, "lbl_expand2");
	GLADE_HOOKUP_OBJECT(link_window, web_expand, "web_expand");
	GLADE_HOOKUP_OBJECT(link_window, vbox2, "vbox2");
	GLADE_HOOKUP_OBJECT(link_window, hbox4, "hbox4");
	//GLADE_HOOKUP_OBJECT (link_window, name_lbl, "name_lbl");
	//GLADE_HOOKUP_OBJECT (link_window, name_entry, "name_entry");
	GLADE_HOOKUP_OBJECT(link_window, hbox3, "hbox3");
	GLADE_HOOKUP_OBJECT(link_window, hyperlink_lbl, "hyperlink_lbl");
	GLADE_HOOKUP_OBJECT(link_window, link_entry, "link_entry");
	GLADE_HOOKUP_OBJECT(link_window, lbl_expand3, "lbl_expand3");
	GLADE_HOOKUP_OBJECT(link_window, hbox2, "hbox2");
	GLADE_HOOKUP_OBJECT(link_window, ok_btn, "ok_btn");
	GLADE_HOOKUP_OBJECT(link_window, cancel_btn, "cancel_btn");

	GtkCellRenderer *renderer;

	GtkTreeViewColumn *col;

	col = gtk_tree_view_column_new();


	gtk_tree_view_column_set_title(col, "Name");

	gtk_tree_view_column_set_reorderable(col, FALSE);



	gtk_tree_view_append_column(GTK_TREE_VIEW(tree_root), col);


	renderer = gtk_cell_renderer_text_new();


	gtk_tree_view_column_pack_start(col, renderer, TRUE);


	gtk_tree_view_column_add_attribute(col, renderer, "text", 0);





	_fileListTree =
	    (GtkWidget *) gtk_tree_store_new(2, G_TYPE_STRING,
					     G_TYPE_STRING);

	gtk_tree_view_set_model(GTK_TREE_VIEW(tree_root),
				GTK_TREE_MODEL(_fileListTree));

	_linkWindow = link_window;
	_expanderLocal = expand_local;
	_expanderFile = expand_file;
	_expanderWeb = web_expand;
	_treeViewLink = tree_root;



	return link_window;
}



void on_ok_btn_activate(GtkButton * button, gpointer user_data)
{
	struct elog_xml_link *lnk = elog_xml_link_new();

	int *locs = (int *) user_data;
	lnk->start = locs[0];
	int pos1 = locs[0];
	lnk->end = locs[1];
	int pos2 = locs[1];

	//lnk->href = 
	//elog_gui_al_addLink(lnk);     
	if (gtk_expander_get_expanded(GTK_EXPANDER(_expanderLocal)) ==
	    TRUE) {
		char *tmp = elog_gui_al_getTreeSelected();
		elog_sp_cat(&(lnk->href), "local://", tmp, NULL);
		lnk->num =
		    edit_gui_al_textLink((int) pos1, (int) pos2,
					 lnk->href);
		edit_gui_io_addLink(lnk);
		edit_gui_io_addUndoActionPtr(EDIT_GUI_IO_ACTION_LINK, lnk,
					     0);
		printf("Link: %s \n", lnk->href);
	} else if (gtk_expander_get_expanded(GTK_EXPANDER(_expanderFile))
		   == TRUE) {
		char *tmp = (char *)
		    gtk_entry_get_text(GTK_ENTRY
				       (lookup_widget
					(_linkWindow, "localFIle_entry")));
		elog_sp_cat(&(lnk->href), "search://", tmp, NULL);
		lnk->num =
		    edit_gui_al_textLink((int) pos1, (int) pos2,
					 lnk->href);
		edit_gui_io_addLink(lnk);
		edit_gui_io_addUndoActionPtr(EDIT_GUI_IO_ACTION_LINK, lnk,
					     0);
	} else if (gtk_expander_get_expanded(GTK_EXPANDER(_expanderWeb)) ==
		   TRUE) {
		char *tmp = (char *)
		    gtk_entry_get_text(GTK_ENTRY
				       (lookup_widget
					(_linkWindow, "link_entry")));
		elog_sp_cat(&(lnk->href), "http://", tmp, NULL);
		lnk->num =
		    edit_gui_al_textLink((int) pos1, (int) pos2,
					 lnk->href);
		edit_gui_io_addLink(lnk);
		edit_gui_io_addUndoActionPtr(EDIT_GUI_IO_ACTION_LINK, lnk,
					     0);
	} else
		elog_err_print_console
		    ("No link option chosen, bombing out of making a link\n");

	on_cancel_btn_activate(button, user_data);	//closes window and whatnot

}


void on_cancel_btn_activate(GtkButton * button, gpointer user_data)
{
	gtk_widget_destroy(_linkWindow);
	gtk_main_quit();
}


void fill_tree(GtkWidget * tree, struct elog_io_dir *list,
	       GtkTreeIter * parent, char *parentStr)
{
	GtkTreeIter child, iter;
	int c;
	int j = 0;
	for (c = 0; list->dir[c] != NULL; ++c) {
		if (list->dir[c][0] == '/') {
			const char *tmp = &(list->dir[c][3]);	// --"///"

			char *full;
			elog_sp_cat(&full, parentStr, "/",
				    &(list->dir[c][3]), "/", NULL);
			if (elog_strct_mode_subdir(full) ==
			    ELOG_STRCT_MODE_MONTH)
				tmp =
				    elog_mnth_str(elog_sp_stringToShort
						  (tmp));

			else if (elog_strct_mode_subdir(full) ==
				 ELOG_STRCT_MODE_OTHER) {
				free(full);
				++j;	//this lil bugger took me a half hour to find
				// but at least I updated some old code while doing it.
				continue;
			}

			gtk_tree_store_append(GTK_TREE_STORE(tree), &iter,
					      parent);
			gtk_tree_store_set(GTK_TREE_STORE(tree), &iter, 0,
					   tmp, 1, full, -1);
			fill_tree(tree, list->subDir[j], &iter, full);
			free(full);
			++j;
		} else {
			char *full;
			elog_sp_cat(&full, list->path, list->dir[c], NULL);
			char *partial = elog_sp_strReplace(full,
							   elog_journ_current
							   ()->settings->
							   path,
							   "/");
			struct elog_xml_doc *doc = elog_crypt_open_dry_noTxt(full);
			if (doc != NULL) {	//is the file null or has a blank subjecT?!
				elog_io_close(doc->handle);
				if (doc->subject != NULL) {
					gtk_tree_store_append
					    (GTK_TREE_STORE(tree), &child,
					     parent);
					gtk_tree_store_set(GTK_TREE_STORE
							   (tree), &child,
							   0, doc->subject,
							   1, partial, -1);
				}
				elog_xml_doc_free(doc);
			}
			free(full);
		}
	}

}


void on_expand_local_activate(GtkExpander * expander, gpointer user_data)
{
	const char *path = elog_set_path(NULL);
	struct elog_io_dir *journ = elog_io_recurseDir(path, path);


	gtk_tree_store_clear(GTK_TREE_STORE(_fileListTree));

	fill_tree(_fileListTree, journ, NULL, "");
	gint x, y;
	gtk_window_get_size(GTK_WINDOW(_linkWindow), &x, &y);
	y = 400;
	gtk_expander_set_expanded(GTK_EXPANDER(_expanderFile), FALSE);
	gtk_expander_set_expanded(GTK_EXPANDER(_expanderWeb), FALSE);
	gtk_window_resize(GTK_WINDOW(_linkWindow), x, y);

	elog_io_dir_free(journ);
}


void on_expand_file_activate(GtkExpander * expander, gpointer user_data)
{
	gtk_expander_set_expanded(GTK_EXPANDER(_expanderLocal), FALSE);
	gtk_expander_set_expanded(GTK_EXPANDER(_expanderWeb), FALSE);
	gint x, y;
	gtk_window_get_size(GTK_WINDOW(_linkWindow), &x, &y);
	y = 180;
	gtk_window_resize(GTK_WINDOW(_linkWindow), x, y);
}


void on_web_expand_activate(GtkExpander * expander, gpointer user_data)
{
	gtk_expander_set_expanded(GTK_EXPANDER(_expanderFile), FALSE);
	gtk_expander_set_expanded(GTK_EXPANDER(_expanderLocal), FALSE);
	gint x, y;
	gtk_window_get_size(GTK_WINDOW(_linkWindow), &x, &y);
	y = 180;
	gtk_window_resize(GTK_WINDOW(_linkWindow), x, y);
}

